package com.qms.tubu.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qms.tubu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mad on 2015/7/16.
 */
public abstract class LoadMoreAdapter<T> extends  RecyclerView.Adapter<RecyclerView.ViewHolder>{
    protected List<T> mList;
    protected Context mContent;
    protected OnItemClickListener onItemClickListener;
    private LoadMoreHolder mLoadMoreHolder;
    public final static byte LOAD_INIT=0;
    public final static byte LOAD_START=1;
    public final static byte LOAD_COMPLETE=2;
    private byte mState;
    public LoadMoreAdapter(Context context)
    {
        mContent=context;
        mList=new ArrayList<T>();
        mState=LOAD_INIT;
    }
    public LoadMoreAdapter(Context context,final GridLayoutManager layoutManager)
    {
        this(context);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
               return position==getItemCount()-1?layoutManager.getSpanCount():1;
            }
        });
    }
    public LoadMoreAdapter(Context context,final GridLayoutManager layoutManager,final boolean hasHead)
    {
        this(context);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if(position==0 && hasHead)
                {
                    return layoutManager.getSpanCount();
                }
                return position == getItemCount() - 1 ? layoutManager.getSpanCount() : 1;
            }
        });
    }
    @Override
    public int getItemViewType(int position) {
       return -1;
    }

    public List<T> getList()
    {
        return mList;
    }

    public void setList(List<T> list) {
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View loadMoreView = LayoutInflater.from(mContent).inflate(R.layout.load_more_footer, parent, false);
            mLoadMoreHolder = new LoadMoreHolder(loadMoreView);
            updateLoadState();
            return mLoadMoreHolder;
    }
    public void setLoadState(byte state)
    {
        mState=state;
        updateLoadState();
    }

    public byte getState() {
        return mState;
    }

    private void updateLoadState()
    {
        if(mLoadMoreHolder==null)
            return;
        switch (mState)
        {
            case LOAD_INIT:
                mLoadMoreHolder.progressBar.setVisibility(View.GONE);
                mLoadMoreHolder.msgText.setText("加载完成");
                break;
            case LOAD_START:
                mLoadMoreHolder.progressBar.setVisibility(View.VISIBLE);
                mLoadMoreHolder.msgText.setText("加载中...");
                break;
            case LOAD_COMPLETE:
                mLoadMoreHolder.progressBar.setVisibility(View.GONE);
                mLoadMoreHolder.msgText.setText("已经没有更多的信息了");
                break;
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return mList.size()+1;
    }
    private class LoadMoreHolder extends RecyclerView.ViewHolder{
        private ProgressBar progressBar;
        private TextView msgText;
        public LoadMoreHolder(View itemView) {
            super(itemView);
            progressBar= (ProgressBar) itemView.findViewById(R.id.load_more_progress);
            msgText= (TextView) itemView.findViewById(R.id.load_more_message_text);
            updateLoadState();
        }

    }
    protected class ClickViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public ClickViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(getAdapterPosition()== RecyclerView.NO_POSITION)
                return;
            if(onItemClickListener!=null)
                onItemClickListener.onItemClick(v,getAdapterPosition(),getItemId());
        }
    }
    public interface OnItemClickListener{
        void onItemClick(View view,int position,long itemId);
    }
}
